﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Coater
{
    public class CommonUiOperator
    {
        public Form mainForm { get; set; }
        Label? currLightLabel;//当前亮灯标签
        int backColorCount = 0; //背景色计数器
        private readonly System.Windows.Forms.Timer? timerWarn;

        public CommonUiOperator(Form mainForm, System.Windows.Forms.Timer? timerWarn)
        {
            this.mainForm = mainForm;
        }

        #region 界面亮灯
        public void SetLightLabelOn(string lightLabelName)
        {
            if (mainForm.IsDisposed)
                return;
            var lightLabel = (Label)mainForm.Controls.Find(lightLabelName, true)[0];
            if (lightLabel == currLightLabel) { return; }
            timerWarn?.Start();
            mainForm.Controls.Find("labYellow", true)[0].BackColor = Color.Yellow;
            mainForm.Controls.Find("labRed", true)[0].BackColor = Color.Red;
            mainForm.Controls.Find("labGreen", true)[0].BackColor = Color.Green;
            mainForm.Controls.Find("labBlue", true)[0].BackColor = Color.Blue;
            currLightLabel = lightLabel;
        }

        public void timerWarn_Tick(object sender, EventArgs e)
        {
            if (this.currLightLabel == null)
            {
                return;
            }
            if (backColorCount > 9999999) backColorCount = 0;
            backColorCount++;
            if (backColorCount % 2 == 1)
                currLightLabel.BackColor = Color.LightGray;
            else
            {
                switch (currLightLabel.Name)
                {
                    case "labYellow":
                        currLightLabel.BackColor = Color.Yellow;
                        break;

                    case "labRed":
                        currLightLabel.BackColor = Color.Red;
                        break;

                    case "labGreen":
                        currLightLabel.BackColor = Color.Green;
                        break;

                    case "labBlue":
                        currLightLabel.BackColor = Color.Blue;
                        break;
                }
            }

        }
        #endregion 界面亮灯

    }
}
